package cn.zmit.kuxi.activity;

import org.w3c.dom.ls.LSInput;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.functions.holder.ListViewDataAdapter;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;
import com.xdroid.widget.loadmore.LoadMoreContainer;
import com.xdroid.widget.loadmore.LoadMoreHandler;
import com.xdroid.widget.loadmore.LoadMoreListViewContainer;

import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.Response;
import cn.zmit.kuxi.entity.OldAnnounceEntity;
import cn.zmit.kuxi.holder.OldAnnounceHolder;
import cn.zmit.kuxi.request.RequestTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

/** * @author @parameter * @return */
public class OldAnnouncedActivity extends BaseActivity implements
		OnRefreshListener, OnScrollListener {
	@ViewInject(R.id.listView_old_announce)
	private ListView listView_old_announce;
	private ListViewDataAdapter<OldAnnounceEntity> oListViewDataAdapter;
	private String goodsId;
	/**
	 * 页码
	 */
	private int mPage = 1;
	@ViewInject(R.id.load_more_list_view_container)
	private LoadMoreListViewContainer mLoadMoreListViewContainer;
	private SwipeRefreshLayout srf_old_annouced;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleView("往期揭晓", true);
		setContentView(R.layout.activity_old_announced);
		ViewUtils.inject(this);
		goodsId = getTextFromBundle("goods_id");
		srf_old_annouced = (SwipeRefreshLayout) findViewById(R.id.srf_old_annouced);
		srf_old_annouced.setOnRefreshListener(this);
		// srf_old_annouced.setEnabled(false);
		srf_old_annouced.setRefreshing(true);
		srf_old_annouced.setColorSchemeResources(R.color.old_announced_refresh);
		// getDate();
		onRefresh();
		initView();
		// initLoadMore();
		listView_old_announce.setOnScrollListener(this);
	}

	// 初始化上拉刷新
	private void initLoadMore() {
		mLoadMoreListViewContainer.useDefaultFooter(); // 使用默认的FooterView
		mLoadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {

			@Override
			public void onLoadMore(LoadMoreContainer loadMoreContainer) {
				if (!mLoadMoreListViewContainer.isLoadError()) {
					mPage++;
					getDate();
				}
			}
		});
	}

	private void getDate() {
		RequestTask.getInstance().getOldAnnounced(this, goodsId, mPage + "",
				new onRequestListener());
	}

	private void initView() {
		oListViewDataAdapter = new ListViewDataAdapter<OldAnnounceEntity>();
		oListViewDataAdapter.setViewHolderClass(this, OldAnnounceHolder.class);
		listView_old_announce.setAdapter(oListViewDataAdapter);
	}

	// 请求
	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			super.onDone(request, response, dataType);
			// oListViewDataAdapter.removeAll();
			Boolean mIsEmpty = false; // 是否是空数据
			Boolean mHasMore = false; // 是否有更多数据待加载
			JsonData jsonData = JsonData.create(response);

			if (jsonData != null && jsonData.length() > 0) {
				JsonData data = jsonData.optJson("winner_list");
				int now_time = jsonData.optInt("now_time");
				if (data.length() > 0) {
					if (data.length() == 8) {
						mHasMore = true; // 有更多数据
					} else if (data.length() < 8) {
						mIsEmpty = false; // 有数据
						mHasMore = false; // 无更多数据
					}
					for (int i = 0; i < data.length(); i++) {
						JsonData itemDate = data.optJson(i);
						String title = itemDate.optString("phase");
						String user_id = itemDate.optString("winner_id");
						String time = itemDate.optString("announceOn");
						int old_time = itemDate.optInt("unix_announceOn");
						String lucky_number = itemDate
								.optString("lucky_number");
						String name = itemDate.optString("name");
						String phone = itemDate.optString("phone");
						String buy_count = itemDate.optString("buy_count");
						OldAnnounceEntity oldAnnounceEntity = new OldAnnounceEntity(
								title, user_id, lucky_number, name, phone,
								buy_count, time, old_time, now_time);
						oListViewDataAdapter.append(oldAnnounceEntity);
					}
				}

			} else {
				ToastUtils.show(context, Response.MASSAGE);
				mIsEmpty = true; // 空数据
				mHasMore = false; // 无更多数据
			}
			// mLoadMoreListViewContainer.loadMoreFinish(mIsEmpty, mHasMore); //
			// 判断是否加载更多
			srf_old_annouced.setRefreshing(false);
		}

		@Override
		public void onRequestPrepare() {
			// TODO Auto-generated method stub
			super.onRequestPrepare();
		}

		@Override
		public void onRequestFailed(String error) {
			// TODO Auto-generated method stub
			super.onRequestFailed(error);
		}
	}

	@Override
	public void onRefresh() {
		// 下拉刷新时候的回调
		if (oListViewDataAdapter != null) {
			oListViewDataAdapter.removeAll();

		}
		mPage = 1;
		getDate();
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		switch (scrollState) {
		case SCROLL_STATE_IDLE:
			if (listView_old_announce.getFirstVisiblePosition() == 0) {
				srf_old_annouced.setEnabled(true);
			} else {
				srf_old_annouced.setEnabled(false);
			}
			if (listView_old_announce.getLastVisiblePosition() == 8 * mPage - 1) {
				mPage++;
				// new Thread() {
				// public void run() {
				// try {
				// Thread.sleep(200);
				// getDate();
				// } catch (InterruptedException e) {
				// e.printStackTrace();
				// }
				// };
				// }.start();
				getDate();
			}
			break;
		case SCROLL_STATE_TOUCH_SCROLL:
			srf_old_annouced.setEnabled(false);
			break;

		case SCROLL_STATE_FLING:
			srf_old_annouced.setEnabled(false);
			break;
		default:
			break;
		}
	}
}
